package sn.ugb.cours.zd.ejb.web.supports;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sn.ugb.cours.zd.ejb.sessions.facades.hello.HelloSessionStatefullLocal;
import sn.ugb.cours.zd.ejb.sessions.facades.hello.HelloSessionStatelessLocal;

public class HelloServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1910389397579238518L;

	@EJB
	private HelloSessionStatelessLocal helloSessionStatelessLocal;
	@EJB
	private HelloSessionStatefullLocal helloSessionStatefullLocal;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		sayHello(req, resp);
		
	}

	private void sayHello(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		fromStateLess(out);
		fromStateFull(out);

	}

	private void fromStateFull(PrintWriter out) {
		if (helloSessionStatefullLocal != null) {
			out.println();
			out.println("fromStateFull");
			String helloWorld = helloSessionStatefullLocal.sayHello();
			helloSessionStatefullLocal.addMessage(" Appel");
			out.print("Lookup réussi : <br/>");
			if (helloWorld != null) {
				out.append(helloWorld);
			}
		} else {
			out.print("Lookup non réussi : <br/>");
		}
	}

	private void fromStateLess(PrintWriter out) {
		if (helloSessionStatelessLocal != null) {
			out.println();
			out.println("fromStateLess");
			String helloWorld = helloSessionStatelessLocal.sayHello();
			helloSessionStatelessLocal.addMessage(" Appel");
			out.print("Lookup réussi : <br/>");
			if (helloWorld != null) {
				out.append(helloWorld);
			}
		} else {
			out.print("Lookup non réussi : <br/>");
		}
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sayHello(req, resp);
	}
}
